package com.cos.hospital.widget;

import android.content.Context;
import android.support.constraint.ConstraintLayout;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;

import com.cos.hospital.R;

public class TopTabView extends ConstraintLayout {
    TabItemView tabItemView1;
    TabItemView tabItemView2;
    int position = 0;
    OnChangeClick onChangeClick;

    public TopTabView(Context context) {
        this(context, null);
    }

    public TopTabView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public TopTabView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        LayoutInflater.from(getContext()).inflate(R.layout.layout_top_tab, this);
        tabItemView1 = findViewById(R.id.tabLayout_1);
        tabItemView2 = findViewById(R.id.tabLayout_2);
        init();
        update();
    }

    private void init() {
        tabItemView1.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                click(0);

            }
        });
        tabItemView2.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                click(1);
            }
        });
    }

    private void click(int i) {
        if (position == i) {
            return;
        }
        position = i;
        update();
        if (onChangeClick != null)
            onChangeClick.onChange(position);
    }

    private void update() {
        tabItemView1.setSelect(position == 0);
        tabItemView2.setSelect(position == 1);

    }

    public void setContent(String label1, String label2) {
        tabItemView1.setLabelContent(label1);
        tabItemView2.setLabelContent(label2);
    }

    public void setPerson1(int str1) {
        tabItemView1.setPersonNum(str1);
    }

    public void setPerson2(int str1) {
        tabItemView2.setPersonNum(str1);
    }

    public interface OnChangeClick {
        void onChange(int positon);
    }

    public void setOnChangeClick(OnChangeClick onChangeClick) {
        this.onChangeClick = onChangeClick;
    }

    public void setSelect(int position) {
        if (this.position == position) {
            return;
        }
        this.position = position;
        update();
    }
}
